from django.http import HttpResponse
from ...models.chat_product import ChatProduct
from django.core.serializers import serialize


from ...utils.time_conversion import iso_to_times
import json


def product_list(request):
    '''
    GET
    list 所有的产品
    '''
    all_chatproduct = ChatProduct.objects.all().order_by('sort_num')

    json_data_str = serialize('json', all_chatproduct)
    json_data = json.loads(json_data_str)

    data = []
    for i in range(len(json_data)):
        json_data[i]['fields']['id'] = json_data[i]['pk']

        # 数据处理
        created_at_second = iso_to_times(json_data[i]['fields']['created_at'])
        updated_at_second = iso_to_times(json_data[i]['fields']['updated_at'])
        json_data[i]['fields']['created_at'] = created_at_second
        json_data[i]['fields']['updated_at'] = updated_at_second

        data.append(json_data[i]['fields'])

    res_mes = {
        "code": 0,
        "data": data
    }
    res_mes_json = json.dumps(res_mes, ensure_ascii=False)
    return HttpResponse(res_mes_json)

